/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject.ExternalUI
{
	internal sealed class FormBuildTrace : BuildTrace
	{
		#region Private / Protected
		private readonly ProgramForm form;
		private string lastStatusText;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="FormBuildTrace" /> class.
		/// </summary>
		public FormBuildTrace(ProgramForm form)
		{
			if (form == null)
				throw new ArgumentNullException("form");

			this.form = form;
		}
		#endregion

		#region Methods
		public override void TraceLine()
		{
			form.TraceBuildLine();
		}

		public override void TraceLine(string message)
		{
			form.TraceBuildLine(message);
		}

		public override void TraceLine(string format, params object[] args)
		{
			form.TraceBuildLine(format, args);
		}

		public override void Trace(string message)
		{
			form.TraceBuild(message);
		}

		public override void Trace(string format, params object[] args)
		{
			form.TraceBuild(format, args);
		}

		public override void TracePath(string path)
		{
			form.TraceBuild(Environment.ExpandEnvironmentVariables(path));
		}

		public override void TracePathLine(string path)
		{
			form.TraceBuildLine(Environment.ExpandEnvironmentVariables(path));
		}

		public override void TraceEvent(System.Diagnostics.TraceEventType type, string category, string message, string file, int line, string userMessage)
		{
			// TODO: create "warnings and errors" list - possibly using a TabControl and a DataGridView
			form.TraceBuildLine(type.ToString() + " [" + category + "]: " + message);
		}

		public override void FlushEvents()
		{
			// do nothing
		}

		public override void SetProgress(string text, int completed, int total)
		{
			form.SetBuildProgress(completed, total);

			if (!string.IsNullOrEmpty(text))
			{
				if (!string.IsNullOrEmpty(lastStatusText))
					form.Status = lastStatusText + " - " + text;
				else
					form.Status = text;
			}
		}

		public override void SetStatus(string text)
		{
			form.Status = lastStatusText = text;
		}
		#endregion
	}
}
